package com.twu.biblioteca;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: aulme
 * Date: 01/07/2013
 * Time: 12:05
 * To change this template use File | Settings | File Templates.
 */
public class HardcodedBookRepository implements BookRepository {
    private List<Book> books;

    public HardcodedBookRepository(List<Book> books) {
        this.books = books;
    }

    @Override
    public Collection<Book> getAll() {
        return books;
    }

    @Override
    public void updateByName(Book bookToUpdate) {
        Book oldBook = findBookByName(bookToUpdate.getName());
        Collections.replaceAll(this.books, oldBook, bookToUpdate);
    }

    @Override
    public Book findBookByName(String name) {
        for (Book book : this.books) {
            if (book.getName().equals(name)) { return book; }
        }
        throw new IllegalArgumentException("No such book found!");
    }
}
